package com.dabai.applicationmanager.adapter;

import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CheckBox;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.dabai.applicationmanager.R;
import com.dabai.applicationmanager.bean.AppBaseInfo;
import com.dabai.applicationmanager.ui.AppDetailsActivity;
import com.dabai.applicationmanager.utils.OtherUtils;
import com.dabai.dbutils.utils.DateUtils;
import com.dabai.dbutils.utils.FileUtils;
import com.github.promeg.pinyinhelper.Pinyin;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import me.zhanghai.android.fastscroll.PopupTextProvider;

public class AppAdapter extends BaseQuickAdapter<AppBaseInfo, BaseViewHolder> implements PopupTextProvider {


    private boolean isSelect;

    public SparseBooleanArray getSelectedPositions() {
        return mSelectedPositions;
    }

    //存放 复选框 bool 值 , 用来批量 维护和获取 复选框
    private SparseBooleanArray mSelectedPositions;


    /**
     * 当前模式是否为选择模式
     *
     * @return boolean
     */
    public boolean isSelect() {
        return isSelect;
    }

    /**
     * 设置选择模式
     *
     * @param select 选择
     */
    public void setSelect(boolean select) {
        if (!isSelect) {
            mSelectedPositions = new SparseBooleanArray();
        }
        isSelect = select;
    }

    /**
     * 设置选中视图 的 数据
     *
     * @param p       p
     * @param isCheck 检查
     */
    public void setCheckData(int p, boolean isCheck) {
        mSelectedPositions.put(p, isCheck);
    }

    /**
     * 设置选中视图 的 复选框
     *
     * @param p       p
     * @param isCheck 检查
     */
    public void setCheckView(int p, boolean isCheck) {
        CheckBox checkBox = (CheckBox) getViewByPosition(p, R.id.checkBox);

        if (checkBox != null) {
            checkBox.setChecked(isCheck);
        }

    }


    /**
     * 得到所有选中的项的 app 信息
     *
     * @return {@link ArrayList<AppBaseInfo>}
     */
    public ArrayList<AppBaseInfo> getCheckAppInflows() {
        ArrayList<AppBaseInfo> result = new ArrayList<>();

        for (int i = 0; i < getData().size(); i++) {
            if (getSelectedPositions().get(i)) {
                result.add(getData().get(i));
            }
        }
        return result;

    }

    /**
     * 得选中应用程序的总大小
     *
     * @return int
     */
    public long getCheckAppSize() {
        ArrayList<AppBaseInfo> result = getCheckAppInflows();
        long total = 0;

        for (AppBaseInfo appBaseInfo : result) {
            File file = new File(appBaseInfo.getPath());
            total += FileUtils.getFileSize(file);
        }
        return total;
    }


    /**
     * 获取选中数据
     *
     * @return {@link ArrayList<Integer>}
     */
    public ArrayList<Integer> getCheckData() {
        ArrayList<Integer> result = new ArrayList<>();

        for (int i = 0; i < getData().size(); i++) {
            if (mSelectedPositions.get(i)) {
                result.add(i);
            }
        }
        return result;
    }

    /**
     * 检查数据大小
     *
     * @return int
     */
    public int getCheckDataSize() {
        return getCheckData().size();
    }


    /**
     * 显示所有复选框
     */
    public void showAllCheckBox() {
        for (int i = 0; i < getData().size(); i++) {
            CheckBox checkBox = (CheckBox) getViewByPosition(i, R.id.checkBox);
            if (checkBox != null) {
                checkBox.setVisibility(View.VISIBLE);
                checkBox.setChecked(false);
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 隐藏所有复选框
     */
    public void hideAllCheckBox() {
        for (int i = 0; i < getData().size(); i++) {
            CheckBox checkBox = (CheckBox) getViewByPosition(i, R.id.checkBox);
            if (checkBox != null) {
                checkBox.setVisibility(View.GONE);
            }
        }
        notifyDataSetChanged();
    }


    /**
     * 选择所有复选框
     */
    public void selectAllCheckBox() {
        for (int i = 0; i < getData().size(); i++) {
            mSelectedPositions.put(i, true);
        }
        notifyDataSetChanged();
    }

    /**
     * 取消选择所有复选框
     */
    public void cancelSelectAllCheckBox() {
        for (int i = 0; i < getData().size(); i++) {
            mSelectedPositions.put(i, false);
        }
        notifyDataSetChanged();
    }

    /**
     * 逆选择所有复选框
     */
    public void inverseSelectAllCheckBox() {
        for (int i = 0; i < getData().size(); i++) {
            mSelectedPositions.put(i, !mSelectedPositions.get(i));
        }
        notifyDataSetChanged();
    }


    public AppAdapter(ArrayList<AppBaseInfo> appBaseInfo) {
        super(R.layout.item_app, appBaseInfo);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull AppBaseInfo appBaseInfo) {

        helper.setText(R.id.textView1, appBaseInfo.getName());
        helper.setText(R.id.textView2, appBaseInfo.getVersion() + "(" + appBaseInfo.getVersionCode() + ")");
        helper.setText(R.id.textView4, FileUtils.formatFileSizeToString(appBaseInfo.getSize()));

        helper.setImageBitmap(R.id.imageView, appBaseInfo.getIcon());


        //因为recyclerview 复用item ,用来维护 checkBox
        if (isSelect) {
            helper.setVisible(R.id.checkBox, true);

            CheckBox checkBox = helper.getView(R.id.checkBox);
            checkBox.setChecked(mSelectedPositions.get(helper.getAdapterPosition()));

        } else {
            helper.setVisible(R.id.checkBox, false);
        }


        helper.findView(R.id.imageView).setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), AppDetailsActivity.class);
            intent.putExtra("packageName", appBaseInfo.getPackageName());
            getContext().startActivity(intent);
        });

    }


    @NonNull
    @Override
    public String getPopupText(int position) {

        switch (OtherUtils.getSortWay(getContext())) {
            case 1:
                return Pinyin.toPinyin(getItem(position).getNameSort().charAt(0));
            case 2:
                return DateUtils.humanInterval(new Date(getItem(position).getLastUpdate()));
            case 3:
                long size = getItem(position).getSize();

                if (size > 1073741824L) {
                    return ">1G";
                } else if (size > 524288000L) {
                    return ">500M";
                } else if (size > 104857600L) {
                    return ">100M";
                } else if (size > 52428800L) {
                    return ">50M";
                } else if (size > 31457280L) {
                    return ">30M";
                } else if (size > 10485760L) {
                    return ">10M";
                } else if (size > 1048576L) {
                    return ">1M";
                } else {
                    return "<1M";
                }
            case 4:
                return "Target API "+getItem(position).getTargetApi();
        }
        return "";
    }
}
